package org.gpp.proj1.test;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.fenggui.Container;
import org.fenggui.composites.Window;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.io.IOStreamException;
import org.fenggui.xml.theme.XMLTheme;
import org.gpp.proj1.menu.fenggui.FengJMEInputHandler;

import com.jme.app.SimpleGame;
import com.jme.input.FirstPersonHandler;
import com.jme.input.MouseInput;

/**
 * Class used for the Easyaccept tests to arrange the menus,
 * supply input and gather output data.
 * 
 * @author Paolo Victor, paolovictor@gmail.com
 */
public class MenuTestCanvas extends SimpleGame implements IButtonPressedListener, JMEInitEventSource {
	
	private List<JMEInitListener> initListeners;
	
	private FengJMEInputHandler fengInput;
	
	private org.fenggui.Display disp;

	private String lastEventTrigger;

	private boolean wasButtonPressed;

	private Map<String, Container> widgets;
	
	public MenuTestCanvas() {
		super();

		this.widgets = new HashMap<String, Container>();
		this.initListeners = new LinkedList<JMEInitListener>();
	}
	
    protected void simpleUpdate() {
        fengInput.update( tpf );
    }
		
    @Override
    protected void simpleRender(){
    	disp.display();
    	disp.setDepthTestEnabled( true );
    }
    
		
	@Override
	protected void simpleInitGame() {
		rootNode.setLocalScale(0.1f);
		rootNode.updateRenderState();
		
		FirstPersonHandler handler =  (FirstPersonHandler) this.input;
		handler.getMouseLookHandler().setEnabled( false );
		
		MouseInput.get().setCursorVisible( true );
		
		this.initGUI();
		
		this.fireJMEInitSuccessfulEvent();
	}
	
	private void initGUI() {
		disp = new org.fenggui.Display(new org.fenggui.render.lwjgl.LWJGLBinding());
		
		try {
			org.fenggui.FengGUI.setTheme(new XMLTheme("data/themes/QtCurve/QtCurve.xml"));
		} catch (IOException e) {
		} catch (IOStreamException e) {
		}
		
		fengInput = new FengJMEInputHandler(disp);
		
		disp.layout();
	}
	
	public void buttonPressed( ButtonPressedEvent event ) {
		this.wasButtonPressed = true;
		this.lastEventTrigger = event.getTrigger().getText();
	}
	
	public void clearEvents() {
		this.wasButtonPressed = false;
		this.lastEventTrigger = null;
	}
	
	public String getLastEventTrigger() {
		return lastEventTrigger;
	}
	
	public boolean wasButtonPressed() {
		return wasButtonPressed;
	}

	public void addInitListener( JMEInitListener listener ) {
		
		this.initListeners.add( listener );
	}

	public void fireJMEInitSuccessfulEvent() {
	
		for( JMEInitListener listener : initListeners ) {
			listener.jmeInitSuccessful();
		}
	}

	public void createWindow(String widgetId, String title, int x, int y, int w, int h) {
		
		Window frame = new Window(false, false, false);
		
		frame.setTitle( title );
		frame.setX( x );
		frame.setY( y );
		frame.setSize( w, h );
		frame.getAppearance().setBorder( null );
		frame.updateMinSize();
		
		disp.addWidget(frame);
		disp.layout();
		
		this.widgets.put( widgetId, frame );
	}
	
	public Container getWidget( String widgetId ) {
		return this.widgets.get( widgetId );
	}

	public FengJMEInputHandler getInputHandler() {
		return this.fengInput;
	}

	public void refresh() {
		
		disp.layout();
	}
}
